﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.VisualStudio.Shell;
using System.Reflection;

namespace Delegen.VSThemeColorsViewer
{
    public partial class ThemeColorsViewerControl : UserControl
    {
        public ThemeColorsViewerControl()
        {
            InitializeComponent();
            LoadColors(String.Empty);
            ListViewColors.ContextMenu = ListViewColors.Resources["ContextMenuColor"] as ContextMenu;
        }

        private void LoadColors(string filterBy)
        {
            List<ThemeColor> themeColors = new List<ThemeColor>();
            Type type = typeof(VsBrushes);
            IEnumerable<PropertyInfo> properties = type.GetProperties(BindingFlags.Public | BindingFlags.Static);
            foreach (PropertyInfo property in properties)
            {
                ThemeColor themeColor = new ThemeColor();
                themeColor.ColorKey = property.Name;
                string brushName = "VsBrush." + property.Name.Remove(property.Name.Length - 3, 3);
                Brush brush = (Brush)FindResource(brushName);
                if (brush is SolidColorBrush)
                {
                    SolidColorBrush solidBrush = brush as SolidColorBrush;
                    Color color = solidBrush.Color;
                    if (String.IsNullOrWhiteSpace(filterBy) || String.CompareOrdinal(String.Format("{0},{1},{2}", color.R.ToString(CultureInfo.InvariantCulture), color.G.ToString(CultureInfo.InvariantCulture), color.B.ToString(CultureInfo.InvariantCulture)), filterBy) == 0)
                    {
                        themeColor.Color = solidBrush;
                        themeColor.RGB = String.Format("{0},{1},{2}", color.R, color.G, color.B);
                        themeColor.FontColor = GetLabelFontColor(Color.FromArgb(color.A, color.R, color.G, color.B));
                        themeColors.Add(themeColor);
                    }
                }
            }
            ListViewColors.ItemsSource = themeColors;
        }

        private void ButtonFilter_Click(object sender, RoutedEventArgs e)
        {
            LoadColors(String.Format("{0},{1},{2}", TextBoxRed.Text, TextBoxGreen.Text, TextBoxBlue.Text));
        }

        private void ButtonRefresh_Click(object sender, RoutedEventArgs e)
        {
            LoadColors(String.Empty);
        }

        private System.Drawing.Bitmap _screenShot;

        private void LabelMouseColor_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Cursor = Cursors.Pen;
            LabelMouseColor.CaptureMouse();
            _screenShot = Win32APICall.GetDesktop();
        }
        private void LabelMouseColor_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Cursor = null;
            LabelMouseColor.ReleaseMouseCapture();
            Point point1 = e.GetPosition(this);
            Point point = PointToScreen(point1);
            System.Drawing.Color myColor = _screenShot.GetPixel((int)point.X, (int)point.Y);
            LabelMouseColor.Background = new SolidColorBrush(Color.FromArgb(myColor.A, myColor.R, myColor.G, myColor.B));
            LabelMouseColor.Foreground = GetLabelFontColor(Color.FromArgb(myColor.A, myColor.R, myColor.G, myColor.B));
            TextBoxRed.Text = myColor.R.ToString(CultureInfo.InvariantCulture);
            TextBoxGreen.Text = myColor.G.ToString(CultureInfo.InvariantCulture);
            TextBoxBlue.Text = myColor.B.ToString(CultureInfo.InvariantCulture);
            ButtonFilter_Click(null, null);
        }

        public Brush GetLabelFontColor(Color backgroundColor)
        {
            var c = backgroundColor;
            var l = 0.2126 * c.ScR + 0.7152 * c.ScG + 0.0722 * c.ScB;
            if (l < 0.5)
            {
                return Brushes.White;
            }
            return Brushes.Black;
        }

        private void MenuItemCopy_Click(object sender, RoutedEventArgs e)
        {
            ThemeColor themeColor = ListViewColors.SelectedItem as ThemeColor;
            if (themeColor != null)
            {
                string colorKey = "VsBrushes." + themeColor.ColorKey;
                Clipboard.SetText(colorKey);
                MessageBox.Show(String.Format("Copied '{0}' to the clipboard.", colorKey), "Copy To Clipboard", MessageBoxButton.OK,
                                MessageBoxImage.Information);
            }
        }
    }
}